package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


@Api(tags = "菜品管理")
@RequestMapping("/admin/dish")
@RestController
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    /**
     * 新增菜品
     * @param dto
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dto){
        dishService.addDish(dto);
        cleanCache("dish"+dto.getCategoryId());
        return Result.success();
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dto){
        log.info("分页查询{}",dto);
        PageResult pageResult = dishService.page(dto);
        return Result.success(pageResult);
    }
    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public Result<DishVO> getDish(@PathVariable Long id){
        log.info("根据id：{}查询",id);
        DishVO dishVO = dishService.getDish(id);
        return Result.success(dishVO);
    }
    /**
     * 修改菜品
     * @param dto
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dto){
        dishService.updateDish(dto);
        cleanCache("dish"+dto.getCategoryId());
        return Result.success();
    }

    /**
     * 批量删除菜品
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
    public Result deleteDish(@RequestParam List<Long> ids){
        dishService.delecteDish(ids);
        cleanCache("dish*");
        return Result.success();
    }

    /**
     * 菜品起售停售
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("菜品起售停售")
    @PostMapping("/status/{status}")
    public Result ControlDishStatus(@PathVariable Integer status,Long id){
        log.info("修改id:{}对应状态ststus:{}",id,status);
        dishService.ControlDishStatus(status,id);
        cleanCache("dish*");
        return Result.success();
    }

    /**
     * 分类查询，用于新增套餐
     * @param categoryId
     * @return
     */
    @ApiOperation("分类查询,用于新增套餐")
    @GetMapping("/list")
    public Result<List<DishVO>> querycategory(Long categoryId){
        log.info("套餐id：{}",categoryId);
        List<DishVO> result = dishService.querycategory(categoryId);
        return Result.success(result);
    }
}
